package com.example.springboottest.student.controller;


import com.example.springboottest.student.service.LeaveActivitiService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("activiti")
public class ActivitiController {
    @Autowired
    private LeaveActivitiService activitiService;

    @ApiOperation(value = "部署流程模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wfName", value = "流程名称", required = true, dataType = "String")
    })
    @RequestMapping(value = "/deployWFTemplate.api", method = {RequestMethod.POST})
    public String deployWFTemplate(@RequestParam(value = "wfName") String wfName) {
        try {
            activitiService.doDeployWFTemplate(wfName);
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
        return "流程模板部署成功";
    }

    @ApiOperation(value = "查看流程模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wfName", value = "流程名称", required = true, dataType = "String")
    })
    @RequestMapping(value = "/viewWFTemplate.api", method = {RequestMethod.POST})
    public Deployment viewWFTemplate(@RequestParam(value = "wfName") String wfName) {
        return activitiService.doViewWFTemplate(wfName);
    }

    @ApiOperation(value = "流程启动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processDefinitionKey", value = "流程名称-例子请假流程的流程定义key是LeaveProcessV3，参数写LeaveProcessV3", required = true, dataType = "String"),
            @ApiImplicitParam(name = "days", value = "请假天数", required = true, dataType = "int")
    })
    @RequestMapping(value = "/startProcess.api", method = {RequestMethod.POST})
    public String startProcess(@RequestParam(value = "processDefinitionKey") String processDefinitionKey,
            @RequestParam(value = "days") Integer days) {
        try {
            activitiService.doStartProcess(processDefinitionKey, days);
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
        return "流程启动成功";
    }

    @ApiOperation(value = "查看正在运行的流程实例")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processDefinitionKey", value = "流程名称-例子请假流程的流程定义key是LeaveProcess，参数写LeaveProcess", dataType = "String")
    })
    @RequestMapping(value = "/searchProcess.api", method = {RequestMethod.POST})
    public List<Map<String, String>> searchProcess(@RequestParam(value = "processDefinitionKey", required = false) String processDefinitionKey) {
        return activitiService.doSearchProcess(processDefinitionKey);
    }

    @ApiOperation(value = "查看历史的流程实例")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processDefinitionKey", value = "不填写查询全部，流程名称-例子请假流程的流程定义key是LeaveProcess，参数写LeaveProcess", dataType = "String")
    })
    @RequestMapping(value = "/searchHistoryProcess.api", method = {RequestMethod.POST})
    public List<Map<String, String>> searchHistoryProcess(@RequestParam(value = "processDefinitionKey", required = false) String processDefinitionKey) {
        return activitiService.doSearchHistoryProcess(processDefinitionKey);
    }

    @ApiOperation(value = "查询任务列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processDefinitionKey", value = "不填写查询全部，流程名称-例子请假流程的流程定义key是LeaveProcess，参数写LeaveProcess", dataType = "String"),
            @ApiImplicitParam(name = "userName", value = "用户名，不填写默认空串，一个也查不到", dataType = "String")
    })
    @RequestMapping(value = "/searchTask.api", method = {RequestMethod.POST})
    public List<String> searchTask(@RequestParam(value = "processDefinitionKey", required = false) String processDefinitionKey,
                                   @RequestParam(value = "userName", required = false) String userName) {
        return activitiService.doSearchTask(processDefinitionKey, userName);
    }

    @ApiOperation(value = "处理任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "流程任务ID，通过查询任务列表得到", required = true, dataType = "String")
    })
    @RequestMapping(value = "/disposeTask.api", method = {RequestMethod.POST})
    public String disposeTask(@RequestParam(value = "taskId") String taskId) {
        try {
            activitiService.doDisposeTask(taskId);
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
        return "任务处理成功";
    }
}